<?php

namespace App\Repositories\Api;

use App\Models\AccountRecord;
use App\Models\Member;
use App\Models\Star;
use App\Models\Trend;
use App\Models\TrendComment;
use App\Repositories\PictureMappingRepository;

class TrendCommentRepository
{
    /**
     * @param array $filter
     * @param int $pageSize
     * @param int $pageNum
     */
    public function getLists($filter = [], $pageSize = 10, $pageNum = 1)
    {
//        sql(false);
        $res = TrendComment::from("trend_comment as tc")
            ->join("trend as t", "t.id", "=", "tc.object_id")
            ->join("member as m", "m.id", "=", "tc.uid")
            ->when(!empty($filter['status']), function ($q) use ($filter) {
                if (is_array($filter['status'])) {
                    return $q->whereIn("tc.status", $filter['status']);
                } else {
                    return $q->where("tc.status", "=", $filter['status']);
                }
            })
            ->whereNull('tc.deleted_at')
            ->where("tc.object_type", $filter['object_type'])
            ->where("tc.object_id", $filter['id'])
            ->where("t.status", 1)
            ->orderBy('tc.id', 'desc')
            ->paginate($pageSize, ['tc.*', 'm.username', 'm.gender', 'm.avatar', 'm.birthday', 'm.occuption'], null, $pageNum)
            ->toArray();

        $res = filterPaginateData($res);

        foreach ($res['items'] as $k => &$v) {
//            $v['like_amount'] = Star::query()->where(['object_type' => "trend_comment", 'object_id' => $v['id']])->count("id");
            $v['is_like'] = Star::query()->where(['object_type' => "trend_comment", 'object_id' => $v['id'], 'uid' => UID])->count("id");
        }

        return $res;
    }

    /*
    //获取评论人
    public function getTrendMember($trend_id = null, $size = 10)
    {
        $model = TrendComment::from("trend_comment as tc")
            ->join("trend as t", "t.id", "=", "tc.trend_id")
            ->join("member as m", "m.id", "=", "tc.uid")
            ->whereNull('tc.deleted_at')
            ->where(["t.status" => 1, 'tc.status' => 1])
            ->where('t.id', $trend_id)
            ->orderBy('tc.id', 'desc');

        $total = $model->count("id");

        $res = $model->limit($size)->get(["avatar"])->distinct("tc.uid")->toArray();

        return ['total' => $total, 'items' => $res];
    }
    */

    public function getOne($id = null)
    {
        if (empty(intval($id)))
            return null;

        $res = Trend::where(['id' => $id])->first();
        if (!empty($res)) {
            $res['member'] = Member::where(['id' => $res['uid']])->first(['id', 'avatar', 'username', 'birthday', 'location_city']);
            $res['pictures'] = (new PictureMappingRepository())->getMappingBySearch(['object_type' => "trend", 'object_id' => $res['id']]);
        }

        return $res;
    }

    /**
     * 新增评论
     * @param $params
     */
    public function addComment($params)
    {
        if (empty($params['content'])) {
            return retFail("评论内容必填");
        }

        if (!intval($params['id'])) {
            return retFail("参数错误");
        }

        $res = TrendComment::create([
            'uid' => UID,
            'content' => "测试评论",
            'object_id' => $params['id'],
            'object_type' => $params['object_type'] ?? "trend",
        ]);

        if ($res) {
            return retSuc(['id' => $res['id']]);
        } else {
            return retFail("评论失败");
        }
    }
}


